package com.wgj.android.ui.activity;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jiaai.xui.XUI;
import com.jiaai.xui.utils.XAppUtils;
import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.utils.log.XLog;
import com.jiaai.xui.utils.permission.XPermissionUtils;
import com.jiaai.xui.view.widget.XAlertMsgDialog;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.entity.Member;
import com.wgj.android.entity.SystemConfg;
import com.wgj.android.entity.Version;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.http.model.AccessToken;
import com.wgj.android.utils.CacheUtils;
import com.wgj.android.utils.DownloadAppUtils;
import com.wgj.android.web.WGJWebActivity;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionNo;
import com.yanzhenjie.permission.PermissionYes;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.util.List;


/**
 * Created by Administrator on 2017/10/31.
 */

public class StartUpActivity extends AppCompatActivity  {

    private static final int REQUEST_CODE_PERMISSION_MULTI = 101;


    private Gson gson=new Gson();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.start_up_activity);
        this.getWindow().setFlags(//去掉Activity上面的状态栏
        WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        AndPermission.with(this)
                .requestCode(REQUEST_CODE_PERMISSION_MULTI)
                .permission(Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE,Manifest.permission.READ_CONTACTS)
                // 你也可以不设置。
                .rationale(new RationaleListener() {
                    @Override
                    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                        // 这里的对话框可以自定义，只要调用rationale.resume()就可以继续申请。
                        AndPermission.rationaleDialog(StartUpActivity.this, rationale).show();
                    }
                })
                .callback(this)
                .start();


    }

    @PermissionYes(REQUEST_CODE_PERMISSION_MULTI)
    public void requestSdcardSuccess(@NonNull List<String> deniedPermissions)
    {
        XHttp.obtain().get(WGJAPI.CHECK_APP_VERSION, null, new HttpCallBack<Version>() {
            @Override
            public void onSuccess(final Version version) {
                CacheUtils.setVersion(StartUpActivity.this,version);
                int oldVersion= XAppUtils.getVersionCode(StartUpActivity.this);
                if(version.getVersion()>oldVersion){
                    new XAlertMsgDialog(StartUpActivity.this,version.getChangelog()).setTitle("发现新版本").setPositiveButton("下载").setListener(new XAlertMsgDialog.OnCloseListener() {
                        @Override
                        public void onClick(Dialog dialog, boolean b) {
                            if(b){
                                DownloadAppUtils.downloadForAutoInstall(StartUpActivity.this,version.getInstallUrl(),"旺旺通app_v"+ version.getVersionShort(),"旺旺通发现新版本");
                            }else {
                                finish();
                            }
                        }
                    }).show();
                }else {
                    initConfig();
                }
            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                XToast.error("服务器或网络异常");
            }
        });

    }

    @PermissionNo(REQUEST_CODE_PERMISSION_MULTI)
    public void requestSdcardFailed(@NonNull List<String> deniedPermissions)
    {
        Toast.makeText(this, "DENY ACCESS SDCARD!", Toast.LENGTH_SHORT).show();
    }
    private  void initConfig(){

        XHttp.obtain().get(WGJAPI.CONFIG_API, null,new HttpCallBack<SystemConfg>() {
            @Override
            public void onSuccess(SystemConfg systemConfg) {
                initUserData();
                CacheUtils.setSystemConfig(systemConfg);
            }
            @Override
            public void onFailed(int i, String s, boolean b) {
                 XToast.error(XUI.getString(R.string.server_error));
            }
        });
    }
    private void  initUserData(){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                AccessToken accessToken=CacheUtils.getToken();
                if(accessToken!=null){
                    Member.init(accessToken.getId(), new HttpCallBack<Member>() {
                        @Override
                        public void onSuccess(Member member) {
                            try {
                                CacheUtils.setMember(member);
                                Intent intent=new Intent(getApplication(),MainActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                                finish();
                            } catch (Exception e) {
                                e.printStackTrace();
                               XLog.e("设置绶存error:"+e.getMessage());
                            }

                        }
                        @Override
                        public void onFailed(int i, String s, boolean b) {
                            Intent intent=new Intent(getApplication(),LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                }else {
                    Intent intent=new Intent(getApplication(),LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        },500);

    }


}
